/*
 * File      : board.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 2009 RT-Thread Develop Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2011-01-13     weety      first version
 */

#include <rtthread.h>
#include <rthw.h>
#include "dm368.h"
#include "uart.h"

#include "board.h"

#include "hardware.h"
#include "psc.h"

/**
 * @addtogroup dm368
 */

/*@{*/

/*
 * Routine: davinci_hd_psc_enable
 * Description:  Enable PSC domains
 */
static void davinci_hd_psc_enable(void)
{
//	unsigned int alwaysonpdnum = 0;

	/* Note this function assumes that the Power Domains are alread on */
//	REG(PSC_MDCTL14) |= 0x03; /* EMAC */
//	REG(PSC_MDCTL15) |= 0x03; /* VDCE */
//	REG(PSC_MDCTL16) |= 0x03; /* Video Port */
//	REG(PSC_MDCTL17) |= 0x03; /* Video Port */
//	REG(PSC_MDCTL20) |= 0x03; /* DDR2 */
//	REG(PSC_MDCTL21) |= 0x03; /* EMIFA */
//	REG(PSC_MDCTL26) |= 0x03; /* UART0 */
//	REG(PSC_MDCTL27) |= 0x03; /* UART1 */
//	REG(PSC_MDCTL28) |= 0x03; /* UART2 */
//	REG(PSC_MDCTL31) |= 0x03; /* I2C */
//	REG(PSC_MDCTL34) |= 0x03; /* TIMER0 */
//	REG(PSC_MDCTL35) |= 0x03; /* TIMER1 */

	/* Set PTCMD.GO to 0x1 to initiate the state transtion for Modules in
	 * the ALWAYSON Power Domain
	 */
//	REG(PSC_PTCMD) = (1<<alwaysonpdnum);

	/* Wait for PTSTAT.GOSTAT0 to clear to 0x0 */
//	while(! (((REG(PSC_PTSTAT) >> alwaysonpdnum) & 0x00000001) == 0))
//		;

//	lpsc_on(DAVINCI_LPSC_GPIO);
	lpsc_on(LPSC_UART0);
	lpsc_on(LPSC_TIMER1);
	
	lpsc_on(0);
	lpsc_on(1);
	lpsc_on(2);
	lpsc_on(3);
	lpsc_on(4);

#ifdef RT_USING_LWIP
	lpsc_on(LPSC_EMAC);
#endif
//	lpsc_on(LPSC_MMC_SD0); //fify	
	lpsc_on(15); //fify	
//rt_kprintf("PINMUX0 = 0x%x\n", REG(SM_PINMUX0));
	/* Enable GIO3.3V cells used for EMAC (???) */
//	rt_kprintf("PERI_CLKCTL = 0x%x\n", REG(SM_VDD3P3V_PWDN));
//	REG(SM_VDD3P3V_PWDN) = 0x80000c0;

	/* Select UART function on UART0 */
//	REG(SM_PINMUX0) &= ~(0x0000003f << 18);
//	REG(SM_PINMUX1) &= ~(0x00000003);

	/* Enable USB */
//	REG(SM_PINMUX0) &= ~(0x80000000);
    
    /* Enable MMC/SD0 IO */
//	REG(SM_PINMUX0) &= ~(1 << 24);
	/* Disable MMC/SD0 IO */
//	REG(SM_PINMUX0) |= (1 << 24);

	/* Set the Bus Priority Register to appropriate value */
//	REG(VBPR) = 0x20;
//    rt_kprintf("PINMUX0 = 0x%x\n", REG(SM_PINMUX0));
}

extern void rt_hw_timer_init(void);
extern void rt_hw_clock_init(void);
extern void rt_hw_mmu_init(void);
//extern void i2c_init(int speed, int slaveadd);

void rt_hw_board_init(void)
{	
	/* Power on required peripherals */
	davinci_hd_psc_enable();

//	rt_hw_clock_init();

#ifdef RT_USING_UART0
	rt_hw_uart_init();
	rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif

	rt_hw_timer_init();	
    	
//    rt_hw_mmu_init();
}

/*@}*/
